package com.fingard.dsp.bank.directbank.sxcb01;

import com.fingard.dsp.bank.directbank.bankInterface.IDeposit;
import com.fingard.dsp.bank.reqrespobj.deposit.Req7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.RetAts7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.detail.RetAts7507Detail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class SXCB01Deposit extends SXCB01Base implements IDeposit {

    @Override
    public RetAts7507DepQry d_7507_DepQry(Req7507DepQry p_req7507) throws Exception {
        RetAts7507DepQry retAts = new RetAts7507DepQry(p_req7507.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            //非公有参数
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            //拼接请求报文
            SXCB01Header header = new SXCB01Header();
            header.tr_code="200108";
            header.req_no = p_req7507.reqHeader.reqSeqID;
            writeCommonHeader(xmlTextWriter,header);
            xmlTextWriter.writeEndElement();
            //获取币种和账号
            XElement body =new XElement("body","");
            body.addChild(new XElement("acno",p_req7507.actNum));//账号
            body.addChild(new XElement("cur_code",convertCurToBank(p_req7507.actCur)));//币种
            xmlTextWriter.writeXElement(body);
            xmlTextWriter.writeEndDocument();
            //拼接报文头
            int length=xmlTextWriter.toXmlString().getBytes(getCharset()).length+2;
            String head=String.format("%-10s",length+"")+"00";
            //发送请求报文
            String[] bankResult = sendToBank(head+xmlTextWriter.toXmlString());
            //WriteBankLogStep2(head+xmlTextWriter.toXmlString());
            //String[] bankResult=new String[]{"","000000055200<ap>    <head>        <tr_code></tr_code>        <org_code>0000</org_code>        <cms_corp_no>10000044</cms_corp_no>        <user_no>123123123</user_no>        <serial_no></serial_no>        <req_no></req_no>        <tr_acdt>20190118</tr_acdt>        <tr_time>085133</tr_time>        <channel>5</channel>        <sign>0</sign>        <file_flag>0</file_flag>        <reserved></reserved>        <succ_flag>1</succ_flag>        <ret_code>9999</ret_code>        <ret_info>????????????????</ret_info>        <ext_info>??????????[&lt;ap&gt;    &lt;h]</ext_info>    </head></ap>"};

            //处理响应报文
            if (bankResult[0].length() == 0) {
                StringBuilder tmpSb = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1].substring(bankResult[1].indexOf("<ap>")), tmpSb);
                if (tmpBkReader != null) {
                    String succFlag= tmpBkReader.getFirstText("/ap/head/succ_flag");
                    String respCode = tmpBkReader.getFirstText("/ap/head/ret_code");
                    String respInfo = tmpBkReader.getFirstText("/ap/head/ret_info");
                    if ("0".equals(succFlag) && "0000".equals(respCode)) {//交易成功
                        if (tmpBkReader.read("/ap/body")) {
                            RetAts7507Detail retAtsDetail = new RetAts7507Detail();
                            retAtsDetail.actArea = p_req7507.actArea;//账号地区区域码
                            retAtsDetail.actNum = tmpBkReader.readedXElement.getSingleTextNotNull("acno");//银行账号
                            retAtsDetail.depositCode = "";//存单号
                            retAtsDetail.currencyID = convertCurToATS(tmpBkReader.readedXElement.getSingleTextNotNull("cur_code"));//币种
                            retAtsDetail.terms = "";
                            retAtsDetail.noteMoney = tmpBkReader.readedXElement.getSingleTextNotNull("balance");
                            retAts.writeRetDetail(retAtsDetail);
                        }
                    }else{
                        tmpRespCode = "-1";
                        tmpRespInfo = respInfo;
                    }
                }else{
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSb.toString();
                }
            }else{
                tmpRespCode = "-1";
                tmpRespInfo = bankResult[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(tmpRespCode) && !retAts.hasWrittenRespCode) {
            retAts = new RetAts7507DepQry(p_req7507.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("查无存单");
        }
        retAts.writeEnd();
        return retAts;
    }
}
